﻿using Dln.Common;
using Dln.Common.Helper;
using Dln.Handler;
using Dln.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Page_Customer_OnHospitalFeeItemList : System.Web.UI.Page
{
    protected string MainUrl = CommonHelper.GetAppSettings("mainurl");
    public static string APPID = CommonHelper.GetAppSettings("AppID");
    public static string time = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
    public static string noncestr = string.Empty, resp = string.Empty;
    public static string wxJsApiParam { get; set; } //H5调起JS API参数
    public object ticket = null;
    public string backUrl = "";

    public string CardID = string.Empty;
    public string PatiName = string.Empty;
    public string SerialnumberAndOrdID = string.Empty;
    public string OrdMoney = string.Empty;
    public string OpenID = "";
    public string visitTime = string.Empty;
    public string OrgName = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        OpenID = GetAuthOpenId();
        //OpenID = "orrZmw6biSbZglMMLFS4eB81nycM";
        CardID = HttpContext.Current.Request.QueryString["CardId"];
        PatiName = HttpContext.Current.Request.QueryString["PatiName"];
        SerialnumberAndOrdID = HttpContext.Current.Request.QueryString["SerialnumberAndOrdID"];
        OrdMoney = HttpContext.Current.Request.QueryString["OrdMoney"];
        visitTime = HttpContext.Current.Request.QueryString["visitTime"];
        OrgName = HttpContext.Current.Request.QueryString["OrgName"];
    }

    protected static string RandString()
    {
        string str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//62个字符
        Random r = new Random();
        string result = string.Empty;

        //生成一个8位长的随机字符，具体长度可以自己更改
        for (int i = 0; i < 16; i++)
        {
            int m = r.Next(0, 62);//这里下界是0，随机数可以取到，上界应该是75，因为随机数取不到上界，也就是最大61，符合我们的题意
            string s = str.Substring(m, 1);
            result += s;
        }

        return result;
    }

    private static string SHA1(string text)
    {
        byte[] cleanBytes = Encoding.Default.GetBytes(text);
        byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes);
        return BitConverter.ToString(hashedBytes).Replace("-", "");
    }

    public string GetAuthOpenId()
    {
        string AppId = CommonHelper.GetAppSettings("AppID");
        string AppSecret = CommonHelper.GetAppSettings("AppSecret");
        string strOpenId = "";
        string name = "";
        string code = DNTRequest.GetString("CODE");

        var cookies = HttpContext.Current.Request.Cookies["openidcookie"];
        if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
        {
            strOpenId = cookies.Value;
        }
        else
        {
            if (!String.IsNullOrEmpty(code))
            {
                string json = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppId, AppSecret, code));
                if (!String.IsNullOrEmpty(json))
                {
                    try
                    {
                        OAuthConfig oAuthConfig = JSONHelper.FromJson<OAuthConfig>(json);
                        if (oAuthConfig != null)
                        {
                            HttpCookie cookie = new HttpCookie("openidcookie");
                            cookie.Value = oAuthConfig.openid;
                            cookie.Expires = DateTime.Now.AddDays(2);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            strOpenId = oAuthConfig.openid;
                        }

                        else
                        {
                            strOpenId = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        strOpenId = "";
                    }
                }
            }
            else
            {
                var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2FPage%2FCustomer%2FOnHospitalFeeItemList.aspx&response_type=code&scope=snsapi_base&state=test#wechat_redirect", AppId, CommonHelper.GetAppSettings("mainurl").Replace(@"://", "%3A%2F%2F"));
                Response.Redirect(url);

            }
        }
        return strOpenId;
    }
}